package com.tz.hotelmanager.controller;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.hotelmanager.domain.HouseExample;
import com.tz.hotelmanager.domain.House;

import com.tz.hotelmanager.domain.User;
import com.tz.hotelmanager.domain.UserExample;
import com.tz.hotelmanager.domain.status;
import com.tz.hotelmanager.mapper.impl.HouseMapperImpl;
import com.tz.hotelmanager.mapper.impl.UserMapperImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tz.hotelmanager.Utils.*;

@RestController()
@RequestMapping("/house/")

public class HouseController {

  @Autowired
  HouseMapperImpl houseMapperImpl;
  @PostMapping(value="getAllHouse")
  public String getAllHouse(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      JSONObject result =new JSONObject();
      List<House> houses =  houseMapperImpl.selectByExample(new HouseExample());
      JSONArray jsonArray= new JSONArray();
      for (House house : houses) {
        jsonArray.add(JSONObject.parse(house.toString()));
      }
      result.put("data", jsonArray);
      return result.toJSONString();
  }
  @PostMapping(value="getAvalHouse")
  public String getAvalHouse(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      JSONObject result =new JSONObject();
      HouseExample houseExample= new HouseExample();
      HouseExample.Criteria criteria= houseExample.createCriteria();
      criteria.andIsbookedEqualTo(0);
      List<House> houses =  houseMapperImpl.selectByExample(houseExample);
      JSONArray jsonArray= new JSONArray();
      for (House house : houses) {
        jsonArray.add(JSONObject.parse(house.toString()));
      }
      result.put("data", jsonArray);
      return result.toJSONString();
  }
  @PostMapping(value="addHouse")
  public String addHotel(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      //TODO: process POST request
      JSONObject result =new JSONObject();

      return result.toJSONString();
  }
  @PostMapping(value="delHouse")
  public String delHouse(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      //TODO: process POST request
      JSONObject result =new JSONObject();
      return result.toJSONString();
  }
  @PostMapping(value="bookHouse")
  public String bookHouse(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      //TODO: Book a house by user 
      JSONObject result =new JSONObject();
      
      JSONObject userbase = JSONObject.parseObject((String) session.getAttribute("userbase"));
      if(userbase.getInteger("type") != 3){
        session.setAttribute("userbase", null);
        result.put("result", status.s_error);
        return result.toJSONString();
      }
      int userid = userbase.getIntValue("id");

      int houseid = jsonparm.getInteger("id");
      HouseExample houseExample = new HouseExample();
      HouseExample.Criteria criteria = houseExample.createCriteria();
      criteria.andIdEqualTo(houseid);
      criteria.andIsbookedEqualTo(0);
      criteria.andIsbusyEqualTo(0);
      List<House> houses = houseMapperImpl.selectByExample(houseExample);
      if(houses.size() <= 0  || houses.get(0).getIsbooked()==1 ||houses.get(0).getIsbusy()==1 ){
        result.put("result", status.s_error);
        return result.toJSONString();
      }
      
      // 定房间 
      houses.get(0).setIsbooked(1);
      houses.get(0).setUserid(userid);

      houseMapperImpl.updateByExampleSelective( houses.get(0), houseExample);
      result.put("result", status.s_ok);
      return result.toJSONString();
  }
  @PostMapping(value="releaseHouse")
  public String releaseHouse(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
      //TODO: process POST request
      JSONObject result =new JSONObject();

      return result.toJSONString();
  }
}